package com.xy.biz.ts.component;

import com.xy.biz.ts.service.TrainService;
import com.xy.lang.domain.Result;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.stereotype.Component;

import javax.annotation.Nullable;

/**
 * @author zgy
 * 扫描培训时间改变状态的定时器
 */
@Component
@Slf4j
public class TrainStatusChangeTask {

    @Autowired
    private TrainService trainService;

    /**
     * 与websocket冲突，需要手动创建
     */
    @Bean
    @Nullable
    public TaskScheduler taskScheduler() {
        ThreadPoolTaskScheduler threadPoolScheduler = new ThreadPoolTaskScheduler();
        threadPoolScheduler.setThreadNamePrefix("SockJS-");
        threadPoolScheduler.setPoolSize(Runtime.getRuntime().availableProcessors());
        threadPoolScheduler.setRemoveOnCancelPolicy(true);
        return threadPoolScheduler;
    }

    /**
     * cron表达式：Seconds Minutes Hours DayOfMonth Month DayOfWeek [Year]
     * 每30分钟扫描一次，扫描培训时间改变状态
     */
    @Scheduled(cron = "0 0/30 * ? * ?")
    private void changeTrainStatus() {
        Result<?> result = trainService.changeStatus();
        log.info("the train status change number is :::" + result.getData());
    }
}
